$import("resource/js/jquery-1.3.2.min.js");
var ystree = new YsTree('ystree');//树对象句柄
ystree.config.showCheckBox = false;//是否启用checkbox

/**
 * 根据服务器返回的XML数据构建树
 * @param {Object} treeNodesXmlData 服务器返回的XML数据
 * @param {Object} clickedObj 被点击的节点
 */
function makeWholeTree(){
    var uri = "ajaxTree.jsp";
    $.ajax({
        type: "POST",
        url: uri,
        data: "reqtimeid=321123&reqtime=" + (new Date()).getTime(),
        dataType: "xml",
        success: function(treeNodesXmlData){
            main(treeNodesXmlData);
        }
    });
    function main(treeNodesXmlData){
	    var treeNodesAmount = $(treeNodesXmlData).find('treenode').length;
	    if(treeNodesAmount<1){
	    	return ;
	    }
	    $(treeNodesXmlData).find('treenode').each(function(i){
		  var jTreeNode = $(this);
		  var node_id = jTreeNode.attr("id");
		  var node_parentid = jTreeNode.attr("parentid");
		  var node_text = jTreeNode.attr("text");
		  var node_value = jTreeNode.attr("value");
	        //ystree.add(id, pid, name, checkboxValue, openNode, url, title, target, icon, iconOpen);
		  ystree.add(node_id, node_parentid, node_text, node_value, false);
	    });
	    $("#ystreeContainer").html(ystree.toString());
    }
}

/**
 * 生成树节点HTML代码的函数
 * @param {Object} jTreeNode 树节点对象（jquery对象）
 * @param {Object} isLastNode 是否是当前节点的最后一个子节点
 */
function getTreeNodeHtml(jTreeNode, isLastNode){
    var newNodeHtml = "";
    if (showCheckBox) {
        newNodeHtml = getTreeNodeHtmlByCheckBox(jTreeNode, isLastNode);
    }
    else {
        newNodeHtml = getTreeNodeHtmlNoCheckBox(jTreeNode, isLastNode);
    }
    return newNodeHtml;
}

/**
 * 生成树节点HTML代码的函数
 * 无checkbox显示需要的情况
 * @param {Object} jTreeNode
 * @param {Object} isLastNode
 */
function getTreeNodeHtmlNoCheckBox(jTreeNode, isLastNode){
    var node_id = jTreeNode.attr("id");
    var node_name = jTreeNode.attr("name");
    var node_parentid = jTreeNode.attr("parentid");
    var node_value = jTreeNode.attr("value");
    var node_text = jTreeNode.attr("text");
    var newNodeHtml = "";
    if (jTreeNode.attr("childNodesAmount") == "0") {
        //无子孙节点
        if (isLastNode) {
            //最后一个子节点
            newNodeHtml += '<span><img src="resource/images/style0/noChildrenLastNode.gif" border="0"/></span>';
        }
        else {
            newNodeHtml += '<span><img src="resource/images/style0/noChildrenMidNode.gif" border="0"/></span>';
        }
        newNodeHtml += '<span><img src="resource/images/style0/file.gif" border="0"/></span>';
    }
    else {
        //有子孙节点
        if (isLastNode) {
            //最后一个子节点
            newNodeHtml += '<img src="resource/images/style0/collapsedLastNode.gif" border="0" onclick="displayNode(this);"/>';
        }
        else {
            newNodeHtml += '<img src="resource/images/style0/collapsedMidNode.gif" border="0"/>';
        }
        newNodeHtml += '<img src="resource/images/style0/folder.gif" border="0"/>';
    }
    newNodeHtml += '&nbsp;<a href="www.sina.com">'+node_text+'</a>';
    return newNodeHtml;
}

/**
 * 生成树节点HTML代码的函数
 * 有checkbox显示需要的情况
 * @param {Object} jTreeNode
 * @param {Object} isLastNode
 */
function getTreeNodeHtmlByCheckBox(jTreeNode, isLastNode){
    var node_id = jTreeNode.attr("id");
    var node_name = jTreeNode.attr("name");
    var node_parentid = jTreeNode.attr("parentid");
    var node_value = jTreeNode.attr("value");
    var node_text = jTreeNode.attr("text");
    var newNodeHtml = "";

    return newNodeHtml;
}
